package utils

import (
	"errors"
	"io"
	"net/http"
	"os"
)

const (
	MaxFileSize = 100 * 1024 * 1024 // 100MB in bytes
)

func extractFileData(filePath string) ([]byte, string, error) {
	file, err := os.Open(filePath)
	if err != nil {
		return nil, "", err
	}
	defer file.Close()

	info, err := file.Stat()
	if err != nil {
		return nil, "", err
	}

	// Check if the file size exceeds 100MB
	if info.Size() > MaxFileSize {
		return nil, "", errors.New("file size exceeds maximum limit (100MB)")
	}

	buf := make([]byte, info.Size())
	_, err = file.Seek(0, 0)
	if err != nil {
		return nil, "", err
	}

	_, err = io.ReadFull(file, buf)
	if err != nil {
		return nil, "", err
	}

	return buf, http.DetectContentType(buf), nil
}
